package com.senac.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;
import com.senac.exception.AlunoException;
import com.senac.exception.AlunoException.ALUNO_ERROR;
import com.senac.model.Aluno;
import com.senac.service.AlunoService;
import com.senac.service.AlunoServiceLocal;

@WebServlet("/aluno")
public class CrudALunoServlet extends GerenciamentoEscolaServlet<Aluno>{

	private static final long serialVersionUID = -2720731095792320876L;
	

	
	protected void cadastrar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		req.getRequestDispatcher("pages/alunoView.jsp").forward(req, resp);
	}
	
	protected void salvar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		
		String idString = req.getParameter("id");
		
		try {
			
			AlunoServiceLocal service = new AlunoService();
			Aluno aluno = mountByRequest(req);
			boolean ok = service.save(aluno);
			
			if(ok) {
				resp.sendRedirect("listarAlunos?msg=salvo");
			} else {
				resp.sendRedirect("listarAlunos?msg=erro_salvar");
			}		
			
		} catch (AlunoException e) {
			
			ALUNO_ERROR error = e.getError();
			
			if(StringUtils.isEmptyOrWhitespaceOnly(idString)) {
				req.getRequestDispatcher("aluno?acao=&msg="+error.toString().toLowerCase()).forward(req, resp);
			} else {
				req.getRequestDispatcher("aluno?acao=editar&id="+idString+"&msg="+error.toString().toLowerCase()).forward(req, resp);
			}
			
		} catch (Exception e) {
			resp.sendRedirect("listarAlunos?msg=erro_salvar");
		}
		
	}
	
	
	protected void editar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		AlunoServiceLocal service = new AlunoService();
		String idString = req.getParameter("id");
		Integer id = Integer.parseInt(idString);
		Aluno aluno = service.getAlunosById(id);
		req.setAttribute("aluno", aluno);
		String url = "pages/alunoView.jsp?" + req.getQueryString();
		req.getRequestDispatcher(url).forward(req, resp);

	}
	
	
	
	protected void excluir(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		AlunoServiceLocal service = new AlunoService();
		String idString = req.getParameter("id");
		Integer id = Integer.parseInt(idString);
		boolean ok = service.delete(id);

		if (ok) {
			resp.sendRedirect("listarAlunos?msg=removido");
		} else {
			resp.sendRedirect("listarAlunos?msg=erro_remover");
		}
	}
	
	
	
	protected Aluno mountByRequest(HttpServletRequest req) {
		Aluno aluno = new Aluno();
		String idString = req.getParameter("id");
		String cargaHoraria = req.getParameter("carga_horaria");
		
		if (!StringUtils.isNullOrEmpty(idString)) 
		{
			aluno.setId(Integer.parseInt(idString));
		}
		
		aluno.setNome(req.getParameter("nome"));
		aluno.setRg(req.getParameter("rg"));

		
		if (!StringUtils.isNullOrEmpty(cargaHoraria)) 
		{
			aluno.setCargaHoraria(Integer.parseInt(cargaHoraria));
		}
		
		return aluno;
	}
	
	protected void listar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		
		AlunoService service = new AlunoService();
		List<Aluno> list = service.getAlunos();
		System.out.println("listar"+list.size());
		req.setAttribute("alunos", list);
		req.getRequestDispatcher("pages/alunoListar.jsp").forward(req, resp);

	}

}
